//% color="#AA888D" iconWidth=50 iconHeight=40
namespace dfr0845_sen0482_v2 {

    //% board="arduino, esp32"
    //% block="Initialize the device, interface [HARDSER] RX:[HARDRX] TX:[HARDTX] address [ADDRESS]" blockType="command"
    //% ADDRESS.shadow="range"  ADDRESS.params.min=0 ADDRESS.params.max=255 ADDRESS.defl=2
    //% HARDSER.shadow="dropdown" HARDSER.options="HARDSER" 
    //% HARDRX.shadow="dropdown" HARDRX.options="HARDRX"
    //% HARDTX.shadow="dropdown" HARDTX.options="HARDTX"
    export function sen0482_init1_v2(parameter: any, block: any) {
        
        let add = parameter.ADDRESS.code
        let ser =  parameter.HARDSER.code;
        let rx = parameter.HARDRX.code;
        let tx = parameter.HARDTX.code;

        Generator.addInclude("RS485_Wind_Direction_Transmitter_V2", "#include <RS485_Wind_Direction_Transmitter_V2.h>");
        Generator.addObject(`windDirection_v2`, `RS485_Wind_Direction_Transmitter_V2`, ` windDirection_v2;`);
        Generator.addObject(`sen0482add_v2`, `uint8_t`, ` sen0482Add_v2 = ${add};`);


        if(Generator.board === 'arduino'){
            Generator.addSetup("sen0482mySerialbegin_v2",`${ser}.begin(9600);`);
        }else if(Generator.board === 'esp32'){
            Generator.addSetup("sen0482mySerialbegin_v2",`${ser}.begin(9600, ${rx}, ${tx});`);
        }
        Generator.addSetup("windDirectionbegin_v2",`windDirection_v2.begin(${ser});`);
        Generator.addSetup("sen0482modify_v2",`windDirection_v2.SetSlaveAddress(0, ${add});`);
    }

    //% board="arduino, microbit, esp32"
    //% block="Initialize the device, interface [SOTFSER] RX:[SOFTRX] TX:[SOFTTX] address [ADDRESS]" blockType="command"
    //% ADDRESS.shadow="range"  ADDRESS.params.min=0 ADDRESS.params.max=255 ADDRESS.defl=2
    //% SOTFSER.shadow="dropdown" SOTFSER.options="SOTFSER"
    //% SOFTRX.shadow="dropdown" SOFTRX.options="SOFTRX" 
    //% SOFTTX.shadow="dropdown" SOFTTX.options="SOFTTX"
    export function sen0482_init2_v2(parameter: any, block: any) {
        
        let add = parameter.ADDRESS.code
        let ser =  parameter.SOTFSER.code;
        let rx = parameter.SOFTRX.code;
        let tx = parameter.SOFTTX.code;

        Generator.addInclude("RS485_Wind_Direction_Transmitter_V2", "#include <RS485_Wind_Direction_Transmitter_V2.h>");
        Generator.addInclude('SoftwareSerial', '#include <SoftwareSerial.h>');
        Generator.addObject(`windDirection_v2`, `RS485_Wind_Direction_Transmitter_V2`, ` windDirection_v2;`);
        Generator.addObject(`sen0482mySerial_v2`, `${ser}`, ` mySerial_v2(${rx},${tx});`);
        Generator.addObject(`sen0482add_v2`, `uint8_t`, ` sen0482Add_v2 = ${add};`);

        Generator.addSetup("sen0482mySerialbegin_v2",`mySerial_v2.begin(9600);`);
        Generator.addSetup("windDirectionbegin_v2",`windDirection_v2.begin(mySerial_v2);`);
        Generator.addSetup("sen0482modify_v2",`windDirection_v2.SetSlaveAddress(0, ${add});`);

    }

    //% board="arduino, microbit, esp32"
    //% block="Read 16 azimuth wind direction" blockType="reporter"
    export function sen0482_getWindDirection_v2(parameter: any, block: any) {
        
        let func = [
			'char *get_direc_v2(int d)',
			'{',
			'   switch(d)',
            '   {',
            '       case 0: return "北";',
            '       case 1: return "东北偏北";',
            '       case 2: return "东北";',
            '       case 3: return "东北偏东";',
            '       case 4: return "东";',
            '       case 5: return "东南偏东";',
            '       case 6: return "东南";',
            '       case 7: return "东南偏南";',
            '       case 8: return "南";',
            '       case 9: return "西南偏南";',
            '       case 10: return "西南";',
            '       case 11: return "西南偏西";',
            '       case 12: return "西";',
            '       case 13: return "西北偏西";',
            '       case 14: return "西北";',
            '       case 15: return "西北偏北";',
            '       default: return "-1";',
            '   }',
			'}',
		];

        Generator.addInclude("RS485_Wind_Direction_Transmitter_V2", "#include <RS485_Wind_Direction_Transmitter_V2.h>");
        Generator.addFunction("get_direc_v2", "char *", "get_direc_v2", "int d", func);
        Generator.addCode(`get_direc_v2(windDirection_v2.GetWindDirection(sen0482Add_v2))`);

    }

    //% board="arduino, microbit, esp32"
    //% block="Read 360 degree wind direction" blockType="reporter"
    export function sen0482_getWindAngle_v2(parameter: any, block: any) {
        
        Generator.addInclude("RS485_Wind_Direction_Transmitter_V2", "#include <RS485_Wind_Direction_Transmitter_V2.h>");
        Generator.addCode(`windDirection_v2.GetWindAngle(sen0482Add_v2)`);

    }

    

}